package com.breeze.blockchain.service.strategy.login;

import static com.breeze.blockchain.common.exception.BizErrorCode.PASSWORD_EMPTY;
import static com.breeze.blockchain.common.exception.BizErrorCode.USERNAME_EMPTY;
import static com.breeze.blockchain.common.exception.BizErrorCode.USERNAME_OR_PASS_ERROR;

import cn.hutool.crypto.digest.DigestUtil;
import com.breeze.blockchain.common.enums.LoginTypeEnum;
import com.breeze.blockchain.mybatis.entity.UserDo;
import com.breeze.blockchain.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 手机号密码登陆
 *
 * @author breeze
 */
@Service
public final class PasswordLoginStrategy implements LoginStrategy {

  @Resource UserService userService;

  @Override
  public LoginTypeEnum loginType() {
    return LoginTypeEnum.PASSWORD;
  }

  @Override
  public UserDo login(String username, String password) {
    USERNAME_EMPTY.notEmpty(username);
    PASSWORD_EMPTY.notEmpty(password);
    UserDo user = userService.getNormalStatusUserByMobile(username);
    user.statusCheck();
    USERNAME_OR_PASS_ERROR.isTrue(DigestUtil.bcryptCheck(password, user.getPassword()));
    return user;
  }
}
